package com.zzyl.controller;


import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.junit.jupiter.api.parallel.ResourceLocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取资源列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> getResourceList(@RequestBody ResourceDto dto){
        List<ResourceVo> resourceVo =resourceService.getResourceList(dto);
        return ResponseResult.success(resourceVo);
    }

    /**
     * 资源树形结构
     * @param itemVo
     * @return
     */
    @PostMapping("/tree")
    public ResponseResult<TreeVo> getResourceTree(@RequestBody TreeItemVo itemVo){
        TreeVo vo = resourceService.getResourceTree(itemVo);
        return ResponseResult.success(vo);
    }

    /**
     * 添加资源
     * @param dto
     * @return
     */
    @PutMapping
    public ResponseResult addResource(@RequestBody ResourceDto dto){
        Resource resource = BeanUtil.toBean(dto, Resource.class);
        resourceService.addResource(resource);
        return ResponseResult.success();
    }

    /**
     * 资源修改
     * @param dto
     * @return
     */
    @PatchMapping
    public ResponseResult updateResource(@RequestBody ResourceDto dto){
        Resource resource = BeanUtil.toBean(dto, Resource.class);
        resourceService.updateResource(resource);
        return ResponseResult.success();
    }

    /**
     * 启用-禁用
     * @param dto
     * @return
     */
    @PostMapping("/enable")
    public  ResponseResult disableResource(@RequestBody ResourceDto dto){
        resourceService.disableResource(dto);
        return ResponseResult.success();
    }

    /**
     * 获取用户菜单栏
     * @return
     */
    @GetMapping("/menus")
    public ResponseResult<List<MenuVo>> getResourceMenus(){
            List<MenuVo> menuVos = resourceService.getResourceMenus();
            return ResponseResult.success(menuVos);
    }

}
